package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcServerMapper;
import com.csun.cmny.provider.model.domain.DmcServer;
import com.csun.cmny.provider.model.dto.server.ServerQueryDto;
import com.csun.cmny.provider.service.DmcServerService;
import com.csun.cmny.util.PublicUtil;
import com.google.common.collect.Lists;
import lombok.Data;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-09-02 14:16
 * @Description: the class dmc server service impl
 */
@Service
public class DmcServerServiceImpl extends BaseService<DmcServer> implements DmcServerService {

    @Resource
    private DmcServerMapper dmcServerMapper;

    /**
     * query dmc server list with page
     *
     * @param serverQueryDto the server query dto
     * @return the list
     */
    @Override
    public List<DmcServer> queryListWithPage(ServerQueryDto serverQueryDto) {

        return dmcServerMapper.queryListWithPage(serverQueryDto);
    }

    /**
     * add dmc server
     *
     * @param dmcServer    the dmc server
     * @param loginAuthDto the login auth dto
     *
     * @return the int.
     */
    @Override
    public Integer add(DmcServer dmcServer, LoginAuthDto loginAuthDto) {

        dmcServer.setUpdateInfo(loginAuthDto);

        return dmcServerMapper.insertSelective(dmcServer);
    }

    /**
     * delete dmc server
     *
     * @param dmcServer    the dmc server
     * @param loginAuthDto the login auth dto
     * @return the int.
     */
    @Override
    public Integer edit(DmcServer dmcServer, LoginAuthDto loginAuthDto) {

        dmcServer.setUpdateInfo(loginAuthDto);

        return dmcServerMapper.updateByPrimaryKeySelective(dmcServer);
    }

    /**
     * query stun server info
     *
     * @return the stun server info（ip1:port1,ip2:port2）
     */
    @Override
    public String queryStunServer() {

        return queryServer(1);
    }

    /**
     * query turn server info
     *
     * @return the turn server info
     */
    @Override
    public String queryTurnServer() {

        return queryServer(2);
    }

    /**
     * query sip server
     *
     * @return the sip server info
     */
    @Override
    public String querySipServer() {

        return queryServer(3);
    }

    public String queryServer(int type) {

        DmcServer dmcServer = new DmcServer();
        dmcServer.setType(type);

        List<DmcServer> dmcServerList = dmcServerMapper.select(dmcServer);
        if (PublicUtil.isEmpty(dmcServerList)) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        for (DmcServer s : dmcServerList) {
            sb.append(s.getIp()).append(":").append(s.getPort()).append(",");
        }

        String str = sb.toString();

        return str.substring(0, str.length() - 1);
    }
}
